<?php

namespace App\Http\Controllers;

//完成任务
use App\Common\Sql;

class FinishTask extends Base
{
    public function logic()
    {
        if (!isset($this->param['uid'])) {
            return $this->returnError(1, "please input uid");
        }
        if (!isset($this->param['type'])) {
            return $this->returnError(2, "please input type");
        }
        if (!isset($this->param['task_id'])) {
            return $this->returnError(3, "please input task_id");
        }
        $uid      = $this->param['uid'];
        $cur_time = time();
        $task_id  = $this->param['task_id'];
        //type：1每日任务 2充值任务
        $type      = $this->param['type'];
        $data      = array();
        $task_info = array();
        if ($type == 1) {
            $row = Sql::select('t_task_conf', [['status', 1], ['id', $task_id]]);
            if (count($row) != 1) {
                return $this->returnError(4, "task_id isn't exists");
            } else {
                $info       = $row[0];
                $begin_time = strtotime($info['begin_time']);
                $end_time   = strtotime($info['end_time']);
                //检查是否在活动时间内
                if ($cur_time < $begin_time || $cur_time > $end_time) {
                    return $this->returnError(5, "The task has expired");
                }
                $taskid = (int)$info['id'];
                $limit  = (int)$info['limit'];
                //从redis里获取用户任务完成状态
                list($unclaimed, $finished) = $this->get_user_task_status($uid, $taskid);
                if ($finished >= $limit) {
                    return $this->returnError(6, "The task has limit");
                } else {
                    if ($unclaimed < 1 || (!$this->sub_task_unclaimed_num($uid, $task_id))) {
                        return $this->returnError(7, "The task can't get reward");
                    } else {
                        $this->add_task_finish_num($uid, $task_id);
                        list($unclaimed, $finished) = $this->get_user_task_status($uid, $taskid);
                        $task_info['task_id']    = $taskid;
                        $task_info['task_name']  = $info['name'];
                        $task_info['limit']      = $limit;
                        $task_info['begin_time'] = $info['begin_time'];
                        $task_info['end_time']   = $info['end_time'];
                        $task_info['unclaimed']  = $unclaimed;
                        $task_info['finished']   = $finished;
                        $reward                  = $info['reward'];
                        //解析任务奖品
                        $rewards = explode('|', $reward);
                        foreach ($rewards as $items) {
                            $item_num = explode('=', $items);
                            if (count($item_num) != 2) {
                                return $this->returnError(8, "The format of reward isn't right");
                            }
                            $reward_id      = (int)$item_num[0];
                            $num            = (int)$item_num[1];
                            $item['itemid'] = $reward_id;
                            $item['num']    = $num;
                            if ($this->reward_item($uid, $reward_id, $num, "每日任务奖品", "task_id:" . (string)$task_id)) {
                                $task_info['reward_list'][] = $item;
                            } else {
                                return $this->returnError(9, "You have a wrong reward!" . (string)$reward_id);
                            }
                        }
                        $data['task_info'] = $task_info;
                    }
                }
            }
        } else if ($type == 2) {
            $row = Sql::select('t_recharge_task_conf', [['status', 1], ['id', $task_id]]);
            if (count($row) != 1) {
                return $this->returnError(4, "task_id isn't exists");
            } else {
                $info       = $row[0];
                $begin_time = strtotime($info['begin_time']);
                $end_time   = strtotime($info['end_time']);
                //检查是否在活动时间内
                if ($cur_time < $begin_time || $cur_time > $end_time) {
                    return $this->returnError(5, "The task has expired");
                }
                $taskid = (int)$info['id'];
                $price  = (int)$info['price'];
                //从redis里获取用户任务完成状态
                $is_finish = $this->get_user_recharge_task_status($uid, $taskid);
                if ($is_finish) {
                    return $this->returnError(6, "The task has limit");//已经领取
                } else {
                    //拿用户充值了多少钱
                    $recharge_num = $this->get_user_recharge_num($uid, $begin_time, $end_time);
                    if ($recharge_num >= $price) {
                        $task_info['task_id']    = $taskid;
                        $task_info['task_name']  = $info['name'];
                        $task_info['price']      = $price;
                        $task_info['begin_time'] = $info['begin_time'];
                        $task_info['end_time']   = $info['end_time'];//等待领取
                        $is_finish               = $this->finish_user_recharge_task($uid, $taskid);
                    } else {
                        return $this->returnError(7, "The task can't get reward");//没资格领取
                    }
                }
                if ($is_finish) {
                    $reward = $info['reward'];
                    //解析任务奖品
                    $rewards = explode('|', $reward);
                    foreach ($rewards as $items) {
                        $item_num = explode('=', $items);
                        if (count($item_num) != 2) {
                            return $this->returnError(8, "The format of reward isn't right");
                        }
                        $reward_id      = (int)$item_num[0];
                        $num            = (int)$item_num[1];
                        $item['itemid'] = $reward_id;
                        $item['num']    = $num;
                        if ($this->reward_item($uid, $reward_id, $num, "充值任务奖品", "task_id:" . (string)$task_id)) {
                            $task_info['reward_list'][] = $item;
                        } else {
                            return $this->returnError(9, "You have a wrong reward!" . (string)$reward_id);
                        }
                    }
                    $data['task_info'] = $task_info;
                }
            }
        }

        $data['uid'] = $uid;
        return $this->returnData($data);
    }

}

